//
//  CZUserAccountViewModel.swift
//  Blog
//
//  Created by whitcast on 16/7/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
// 封装OAuth登录相关接口
class CZUserAccountViewModel: NSObject {
    //单例访问点 创建了单例
    static let shareUserAccount:CZUserAccountViewModel = CZUserAccountViewModel()
    // 获取用户对象的从沙盒路径里面解档对象 只读计算属性
    var userAccount: CZUserAccount? {
        // 解档用户对象
        return CZUserAccount.loadUserAccount()
    }
    
    // 构造函数私有化
    private override init() {
        super.init()
    }
    
    // 封装判断accessToken是否为nil，表示登录结果
    var islogin: Bool {
        return accessToken != nil
    }
    
    var accessToken: String? {
        guard let token = userAccount?.access_token else {
            return nil
        }
        
        let result = userAccount?.expiresDate?.compare(NSDate())
        // 使用降序表示我们进行从大到小
        if result == NSComparisonResult.OrderedDescending {
            return token
        } else {
            return nil
        }
    }
    //通过授权码(code)获取accesstoken
    func requestAccesstoken(code: String, callBack: (isSuccess:Bool)->()) {
        CZNetworkTools.sharedTools.requestAccesstoken(code) { (response, error) -> () in
            
            if error != nil {
                print(error)
                callBack(isSuccess: false)
                return
            }
            
            // 代码执行到此，成功的回调
            guard let dic = response as? [String: AnyObject] else {
                print("不是一个正确的json格式")
                callBack(isSuccess: false)
                return
            }
            
            // 代码执行到此, dic一定有值,并且是符合我们需要的类型
            let userAccount = CZUserAccount(dic: dic)
            print(userAccount)
            
            // 通过accessToken和uid获取用户信息
            self.requestUserInfo(userAccount, callBack: callBack)
            
           
        }
    }
    
    private func requestUserInfo(userAccount: CZUserAccount, callBack: (isSuccess: Bool)->()) {
        
        CZNetworkTools.sharedTools.requestUserInfo(userAccount) { (response, error) -> () in
            
            if error != nil {
                print(error)
                callBack(isSuccess: false)
                return
            }
            
            guard let dic = response as? [String: AnyObject] else {
                print("不是一个正确json格式")
                callBack(isSuccess: false)
                return
            }
            // 代码执行到此字典一定有值
            
            let name = dic["name"]
            let avatar_large = dic["avatar_large"]
            
            // 设置用户名和头像
            userAccount.name = name as? String
            userAccount.avatar_large = avatar_large as? String
            
            // 保存用户头像
            let result = userAccount.saveUserAccount()
            
            if result {
                callBack(isSuccess: true)
            }else {
                callBack(isSuccess: false)
            }
            print(userAccount)
        }
    }
}


